package com.dopies.bsdmall.fragment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dopies.BeeFramework.fragment.BaseFragment;
import com.dopies.BeeFramework.model.BusinessResponse;
import com.dopies.BeeFramework.view.WebImageView;
import com.dopies.bsdmall.R;
import com.dopies.bsdmall.activity.AddressManageActivity;
import com.dopies.bsdmall.activity.CheckCashActivity;
import com.dopies.bsdmall.activity.CollectActivity;
import com.dopies.bsdmall.activity.HelpListActivity;
import com.dopies.bsdmall.activity.LoginActivity;
import com.dopies.bsdmall.activity.RecommendUsersActivity;
import com.dopies.bsdmall.activity.SettingActivity;
import com.dopies.bsdmall.activity.ShopNotifyActivity;
import com.dopies.bsdmall.activity.TradeActivity;
import com.dopies.bsdmall.model.ProtocolConst;
import com.dopies.bsdmall.model.UserInfoModel;
import com.dopies.bsdmall.protocol.USER;
import com.external.activeandroid.query.Select;
import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListView;
import com.external.maxwin.view.XListView.IXListViewListener;

/**
 * 身份主页
 */
public class ProfileFragment extends BaseFragment implements
		IXListViewListener, OnClickListener, BusinessResponse {

	private File file;

	private View view;
	private View headView;
	private XListView xlistView;

	private ImageView setting;
	private WebImageView photo;
	private ImageView camera;

	private TextView name;
	private FrameLayout payment;
	private TextView payment_num;
	private FrameLayout ship;
	private TextView ship_num;
	private FrameLayout receipt;
	private TextView receipt_num;
	private FrameLayout history;
	private TextView history_num;
	private TextView collect_num;
	private TextView score_num;
	private TextView profile_id;
	private LinearLayout recommend_num;
	private TextView recommend_num_value;

	private LinearLayout collect;
	private LinearLayout score;
	private LinearLayout notify;
	private LinearLayout address_manage;

	private LinearLayout memberLevelLayout;
	private TextView memberLevelName;
	private ImageView memberLevelIcon;

	private LinearLayout callLayout;

	private LinearLayout help;

	private USER user;
	private UserInfoModel userInfoModel;

	private SharedPreferences shared;
	private SharedPreferences.Editor editor;

	private ImageView image;
	private String uid;
	private boolean isRefresh = false;

	private String user_id;
	private String user_name;
	private String parent_id;

	protected Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		view = inflater.inflate(R.layout.profile, null);

		mContext = getActivity();

		shared = getActivity().getSharedPreferences("userInfo", 0);
		editor = shared.edit();

		headView = LayoutInflater.from(getActivity()).inflate(
				R.layout.profile_head, null);

		image = (ImageView) view.findViewById(R.id.profile_setting);
		image.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), SettingActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
		});

		xlistView = (XListView) view.findViewById(R.id.profile_list);
		xlistView.addHeaderView(headView);

		xlistView.setPullLoadEnable(false);
		xlistView.setRefreshTime();
		xlistView.setXListViewListener(this, 1);
		xlistView.setAdapter(null);

		callLayout = (LinearLayout) headView.findViewById(R.id.call);

		memberLevelLayout = (LinearLayout) headView
				.findViewById(R.id.member_level_layout);
		memberLevelName = (TextView) headView.findViewById(R.id.member_level);
		memberLevelIcon = (ImageView) headView
				.findViewById(R.id.member_level_icon);

		setting = (ImageView) headView.findViewById(R.id.profile_head_setting);
		photo = (WebImageView) headView.findViewById(R.id.profile_head_photo);
		camera = (ImageView) headView.findViewById(R.id.profile_head_camera);
		name = (TextView) headView.findViewById(R.id.profile_head_name);

		payment = (FrameLayout) headView
				.findViewById(R.id.profile_head_payment);
		payment_num = (TextView) headView
				.findViewById(R.id.profile_head_payment_num);
		recommend_num = (LinearLayout) headView
				.findViewById(R.id.recommend_num);
		recommend_num_value = (TextView) headView
				.findViewById(R.id.recommend_num_value);

		ship = (FrameLayout) headView.findViewById(R.id.profile_head_ship);
		ship_num = (TextView) headView.findViewById(R.id.profile_head_ship_num);

		receipt = (FrameLayout) headView
				.findViewById(R.id.profile_head_receipt);
		receipt_num = (TextView) headView
				.findViewById(R.id.profile_head_receipt_num);

		history = (FrameLayout) headView
				.findViewById(R.id.profile_head_history);
		history_num = (TextView) headView
				.findViewById(R.id.profile_head_history_num);

		collect = (LinearLayout) headView
				.findViewById(R.id.profile_head_collect);
		score = (LinearLayout) headView.findViewById(R.id.profile_head_score);

		notify = (LinearLayout) headView.findViewById(R.id.profile_head_notify);
		address_manage = (LinearLayout) headView
				.findViewById(R.id.profile_head_address_manage);
		collect_num = (TextView) headView
				.findViewById(R.id.profile_head_collect_num);
		score_num = (TextView) headView
				.findViewById(R.id.profile_head_score_num);
		profile_id = (TextView) headView
				.findViewById(R.id.profile_head_id_value);
		help = (LinearLayout) headView.findViewById(R.id.profile_help);

		setting.setOnClickListener(this);
		camera.setOnClickListener(this);
		payment.setOnClickListener(this);
		ship.setOnClickListener(this);
		receipt.setOnClickListener(this);
		history.setOnClickListener(this);
		collect.setOnClickListener(this);
		score.setOnClickListener(this);
		profile_id.setOnClickListener(this);
		notify.setOnClickListener(this);
		address_manage.setOnClickListener(this);
		name.setOnClickListener(this);
		help.setOnClickListener(this);
		callLayout.setOnClickListener(this);
		recommend_num.setOnClickListener(this);

		File files = new File(getActivity().getCacheDir() + "/ECMobile/cache"
				+ "/temp.jpg");
		if (files.exists()) {
			photo.setImageBitmap(BitmapFactory.decodeFile(getActivity()
					.getCacheDir() + "/ECMobile/cache" + "/temp.jpg"));
		} else {
			photo.setImageResource(R.drawable.profile_no_avarta_icon);
		}

		if (null == userInfoModel) {
			userInfoModel = new UserInfoModel(getActivity());
		}
		userInfoModel.addResponseListener(this);

		uid = shared.getString("uid", "");
		if (uid.equals("")) {

			Resources resource = mContext.getResources();

			String click = resource.getString(R.string.click_to_login);
			name.setText(click);
			camera.setVisibility(View.GONE);
			memberLevelLayout.setVisibility(View.GONE);

		} else {
			userInfoModel.getUserInfo();
			camera.setVisibility(View.VISIBLE);
			memberLevelLayout.setVisibility(View.VISIBLE);
		}
		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		uid = shared.getString("uid", "");
		if (!uid.equals("")) {
			if (isRefresh == true) {
				userInfoModel.getUserInfo();
			}
			camera.setVisibility(View.VISIBLE);
		} else {
			camera.setVisibility(View.GONE);
		}
		isRefresh = false;
	}

	// set User 信息
	public void setUserInfo() {
		name.setText(user.name);
		memberLevelName.setText(user.rank_name);
		memberLevelLayout.setVisibility(View.VISIBLE);
		if (user.rank_level != UserInfoModel.RANK_LEVEL_NORMAL) {
			memberLevelIcon.setVisibility(View.VISIBLE);
		} else {
			memberLevelIcon.setVisibility(View.GONE);
		}

		if (!user.order_num.await_pay.equals("0")) {
			payment_num.setVisibility(View.VISIBLE);
			payment_num.setText(user.order_num.await_pay);
		} else {
			payment_num.setVisibility(View.GONE);
		}

		if (!user.order_num.await_ship.equals("0")) {
			ship_num.setVisibility(View.VISIBLE);
			ship_num.setText(user.order_num.await_ship);
		} else {
			ship_num.setVisibility(View.GONE);
		}

		if (!user.order_num.shipped.equals("0")) {
			receipt_num.setVisibility(View.VISIBLE);
			receipt_num.setText(user.order_num.shipped);
		} else {
			receipt_num.setVisibility(View.GONE);
		}

		if (!user.order_num.finished.equals("0")) {
			history_num.setVisibility(View.VISIBLE);
			history_num.setText(user.order_num.finished);
		} else {
			history_num.setVisibility(View.GONE);
		}

		collect_num.setText(user.collection_num);
		score_num.setText(user.score_num);
		profile_id.setText(user.id);
		recommend_num_value.setText(user.recommend_num);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()) {
		case R.id.profile_head_setting:
			uid = shared.getString("uid", "");
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(getActivity(), SettingActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
			break;
		case R.id.profile_head_camera:
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, 1);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
			break;
		case R.id.profile_head_payment:
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(getActivity(), TradeActivity.class);
				intent.putExtra("flag", "await_pay");
				startActivityForResult(intent, 2);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
			break;
		case R.id.profile_head_ship:
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(getActivity(), TradeActivity.class);
				intent.putExtra("flag", "await_ship");
				startActivityForResult(intent, 2);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
			break;
		case R.id.profile_head_receipt:
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(getActivity(), TradeActivity.class);
				intent.putExtra("flag", "shipped");
				startActivityForResult(intent, 2);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
			break;
		case R.id.profile_head_history:
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(getActivity(), TradeActivity.class);
				intent.putExtra("flag", "finished");
				startActivityForResult(intent, 2);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
			break;
		case R.id.profile_head_collect:
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(getActivity(), CollectActivity.class);
				startActivityForResult(intent, 2);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
			break;
		case R.id.profile_head_score:
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(getActivity(), CheckCashActivity.class);
				startActivityForResult(intent, 2);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
			break;
		case R.id.profile_head_notify:
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(getActivity(), ShopNotifyActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
			break;
		case R.id.profile_head_address_manage:
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(getActivity(), AddressManageActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			}
			break;
		case R.id.profile_head_name:
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			}
			break;
		case R.id.profile_help: {
			intent = new Intent(getActivity(), HelpListActivity.class);
			startActivity(intent);
			getActivity().overridePendingTransition(R.anim.push_right_in,
					R.anim.push_right_out);
			break;
		}
		case R.id.call: {
			Uri uri = Uri.parse("tel:02552103211");
			intent = new Intent(Intent.ACTION_DIAL, uri);
			startActivity(intent);
			break;
		}
		case R.id.recommend_num: {
			if (uid.equals("")) {
				isRefresh = true;
				intent = new Intent(getActivity(), LoginActivity.class);
				intent.putExtra("user_id", uid);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {
				intent = new Intent(getActivity(), RecommendUsersActivity.class);
				startActivityForResult(intent, 100);
			}
			break;
		}
		}
	}

	@Override
	public void onRefresh(int id) {
		// TODO Auto-generated method stub

		if (!uid.equals("")) {
			userInfoModel.getUserInfo();
		}

	}

	@Override
	public void onLoadMore(int id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == 100) { // 推荐人信息回调
			if (data != null) {
				user_id = data.getStringExtra("user_id");
				user_name = data.getStringExtra("user_name");
				parent_id = data.getStringExtra("parent_id");
			}
		}

		if (resultCode == Activity.RESULT_OK) {

			if (requestCode == 1) {
				String sdStatus = Environment.getExternalStorageState();
				Bundle bundle = data.getExtras();
				Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据，并转换为Bitmap图片格式

				if (file == null) {
					file = new File(ProtocolConst.FILEPATH);
					if (!file.exists()) {
						file.mkdirs();
					}
				}
				FileOutputStream b = null;
				String fileName = getActivity().getCacheDir()
						+ "/ECMobile/cache" + "/temp.jpg";
				try {
					b = new FileOutputStream(fileName);
					bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} finally {
					try {
						b.flush();
						b.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				((ImageView) view.findViewById(R.id.profile_head_photo))
						.setImageBitmap(bitmap);// 将图片显示在ImageView里
			}

		}

		if (requestCode == 2) {
			userInfoModel.getUserInfo();
		}

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status) {
		if (url.endsWith(ProtocolConst.USERINFO)) {
			xlistView.stopRefresh();
			xlistView.setRefreshTime();
			user = userInfoModel.user; // 从网络获取
			setUserInfo();
		}
	}

	public static USER userInfo(String uid) {
		return new Select().from(USER.class).where("USER_id = ?", uid)
				.executeSingle();
	}

	@Override
	public void onDestroy() {
		userInfoModel.removeResponseListener(this);
		super.onDestroy();
	}
}
